﻿//
//  Monkey Manager
//  Copyright (C) 2010 Victor Tereschenko (aka sibvic)
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//     
// ========================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace ManagerDB.Utils
{
    public class FileNameGenerator
    {
        public FileNameGenerator(string fileName)
        {
            if (fileName != null)
            {
                string fullFileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
                loadReplaceList(fullFileName);
            }
        }

        class ReplaceData
        {
            public char Ch;
            public string Replacement;
        }

        private List<ReplaceData> mReplacements = new List<ReplaceData>();
        string mSkip = "";
        private void loadReplaceList(string fileName)
        {
            if (!File.Exists(fileName))
                return;

            XmlDocument doc = new XmlDocument();
            doc.Load(fileName);
            XmlNodeList replacementsNodes = doc.SelectNodes("//replace");
            foreach (XmlNode node in replacementsNodes)
            {
                ReplaceData data = new ReplaceData();
                data.Ch = node.Attributes["ch"].Value[0];
                data.Replacement = node.Attributes["repl"].Value;
                mReplacements.Add(data);
            }
            XmlNode skipNode = doc.SelectSingleNode("replacements/skip");
            if (skipNode != null)
                mSkip = skipNode.Attributes["chars"].Value;
        }

        private string replaceChar(char ch)
        {
            if (mSkip.Contains(ch))
                return ch.ToString();
            ReplaceData data = mReplacements.Find(delegate(ReplaceData d) { return d.Ch == ch; });
            if (data != null)
                return data.Replacement;

            return ch.ToString();
        }

        public string GenerateName(string name, FileDescription file)
        {   
            string extension = Path.GetExtension(file.FileName);
            string result = GenerateName(name, extension);
            if (result == null)
                return file.FileName;

            return result;
        }

        public string GenerateName(string name, string extension)
        {
            if (mReplacements.Count == 0)
                return null;

            StringBuilder newFileName = new StringBuilder();
            for (int i = 0; i < name.Length; ++i)
            {
                string replacement = replaceChar(name[i]);
                newFileName.Append(replacement);
            }
            while (newFileName.ToString().Contains(".."))
                newFileName.Replace("..", ".");
            if (newFileName.Length > 200)
                newFileName.Remove(200, newFileName.Length - 200);
            if (newFileName.ToString().EndsWith("."))
                newFileName.Remove(newFileName.Length - 1, 1);
            newFileName.Append(extension);
            return newFileName.ToString();
        }
    }
}
